package com.cool.modules.finance.controller.admin;

import cn.hutool.json.JSONObject;
import com.cool.core.annotation.CoolRestController;
import com.cool.core.base.BaseController;
import com.cool.core.request.R;
import com.cool.core.util.CoolSecurityUtil;
import com.cool.modules.finance.entity.category.FinanceCategoryEntity;
import com.cool.modules.finance.entity.category.table.FinanceCategoryEntityTableDef;
import com.cool.modules.finance.entity.family.table.FinanceUserFamilyEntityTableDef;
import com.cool.modules.finance.service.category.FinanceCategoryService;
import com.cool.modules.finance.service.family.FinanceUserFamilyService;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;

@Tag(name = "记账分类", description = "记账分类")
@CoolRestController(api = { "add", "delete", "update", "list","page" })
@RequiredArgsConstructor
public class AdminFinanceCategoryController extends BaseController<FinanceCategoryService, FinanceCategoryEntity> {

    final FinanceUserFamilyService userFamilyService;

    @Override
    protected void init(HttpServletRequest request, JSONObject requestParams) {
        FinanceCategoryEntityTableDef category = FinanceCategoryEntityTableDef.FINANCE_CATEGORY_ENTITY.as("finance_category");
        FinanceUserFamilyEntityTableDef family = FinanceUserFamilyEntityTableDef.FINANCE_USER_FAMILY_ENTITY.as("family");

        Long familyId = userFamilyService.getFamilyIdByUserId(CoolSecurityUtil.getCurrentUserId());

        setPageOption(
                createOp()
                        .keyWordLikeFields(
                                FinanceCategoryEntityTableDef.FINANCE_CATEGORY_ENTITY.CATEGORY_NAME,
                                FinanceCategoryEntityTableDef.FINANCE_CATEGORY_ENTITY.CATEGORY_CODE).
                        queryWrapper(new QueryWrapper().from(category).
                                where(category.FAMILY_ID.eq(familyId).when(familyId!=null))));

        setListOption(createOp().queryWrapper(new QueryWrapper().from(category).leftJoin(family).
                on(category.FAMILY_ID.eq(family.FAMILY_ID)).
                where(family.USER_ID.eq(CoolSecurityUtil.getCurrentUserId())).
                orderBy(FinanceCategoryEntity::getHotOrder,false)));
    }

    @Operation(summary = "分类列表(接口调用)", description = "分类列表(接口调用)")
    @PostMapping("/categoryList")
    protected R categoryList(@RequestAttribute() JSONObject requestParams) {
        FinanceCategoryEntityTableDef category = FinanceCategoryEntityTableDef.FINANCE_CATEGORY_ENTITY.as("finance_category");
        FinanceUserFamilyEntityTableDef family = FinanceUserFamilyEntityTableDef.FINANCE_USER_FAMILY_ENTITY.as("family");

        QueryWrapper query = new QueryWrapper();
        query.from(category).leftJoin(family).
                on(category.FAMILY_ID.eq(family.FAMILY_ID)).
                where(family.USER_ID.eq(CoolSecurityUtil.getCurrentUserId())).
                orderBy(FinanceCategoryEntity::getHotOrder,false);
        return R.ok(service.list(query));
    }
}
